import pandas as pd
from sklearn.svm import SVR, LinearSVR
import warnings
from sklearn.model_selection import KFold
import matplotlib.pyplot as plt
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
import numpy as np
warnings.filterwarnings('ignore')

version = 8
all_label = pd.read_csv('data/{version}all_label.csv'.format(version=version), header=0)
all_fea_season = pd.read_csv('data/{version}all_fea_season.csv'.format(version=version), header=None)
all_fea_month_tst = pd.read_csv('data/{version}all_fea_month_tst.csv'.format(version=version), header=0)
pred2017 = pd.read_csv('res/1-res0912.csv', header=None)
pred2017.set_index(0, inplace=True)
header = pd.read_csv('data/{version}all_fea_header.csv'.format(version=version), header=0, index_col=0)


X_trn = all_fea_season
X_tst = all_fea_month_tst

std = all_fea_season.std(axis=0)
# sel_cols = std > 0
# sel_all = all_fea_month.ix[:, sel_cols]
# std = std[sel_cols]
# men = sel_all.mean(axis=0)
# X_trn = (sel_all - men) / std
# sel_tst = all_fea_month_tst.ix[:, sel_cols]
# X_tst = (sel_tst-men)/std

cols = std.sort_values(ascending=False).index.to_list()
# cols = list(map(str, range(std.shape[0])))

kf = KFold(n_splits=10, shuffle=True, random_state=1)  # 此时只随机一次，伪随机
model = SVR()
# model = LinearSVR(C=10)
model = GradientBoostingRegressor()
# model = RandomForestRegressor()



x1 = []
y1 = []
glabal_bstacc = float(np.inf)
for i in range(5, 7*version, 1):
    bst_acc = float(np.inf)
    Acc = []
    X = X_trn.ix[:, cols[:i]]
    x1 += [i]
    for train_index, test_index in kf.split(X):
        X_train, y_train = X.ix[train_index, :], all_label.ix[train_index, :]
        X_test, y_test = X.ix[test_index, :], all_label.ix[test_index, :]

        model.fit(X_train, y_train)
        pred = model.predict(X_test)
        acc = sum((pred - y_test.values.T[0]) ** 2) / 2 / len(pred)
        Acc += [acc]
        # print(acc)
        if acc < bst_acc:
            bst_acc = acc
            if bst_acc < glabal_bstacc:
                global_bstacc = bst_acc
                model_bst = model
    print(bst_acc)
    y1 += [bst_acc]

plt.plot(x1, y1)
plt.show()

# feat_imp = model_bst.feature_importances_
# sort_ind = feat_imp.argsort()[::-1]
# A = pd.concat([header.ix[sort_ind, :].reset_index().drop('index', axis=1), pd.DataFrame(feat_imp[sort_ind])], axis=1, ignore_index=True)
# A.to_csv('model/{version}coe.csv'.format(version=version))

# pred = model_bst.predict(all_fea_month_tst)
# pred_df = pd.DataFrame(pred)
# pred_df.index = pred2017.index
# result = pred2017.ix[:, 1] + pred_df.ix[:, 0]
# result.to_csv('res/13-1.csv', header=False, index=True)
